package com.jmebananas.input.action;

import com.jme.input.InputHandler;
import com.jme.input.Mouse;
import com.jme.input.MouseInput;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyStrafeLeftAction;
import com.jme.input.action.KeyStrafeUpAction;
import com.jme.input.action.MouseInputAction;
import com.jme.renderer.Camera;
import com.jme.system.DisplaySystem;

/**
 * MousePan
 * 
 * @author Glenn Powell
 * 
 */
public class MousePanAction extends MouseInputAction {

	private KeyStrafeUpAction keyUp;

	private KeyStrafeLeftAction keyLeft;

	private InputActionEvent event;

	private Camera camera;
	
	private int grabButton;


	public MousePanAction(Mouse mouse, Camera camera, float speed, int grabButton) {
		this.mouse = mouse;
		this.speed = speed;
		this.grabButton = grabButton;
		this.camera = camera;

		keyUp = new KeyStrafeUpAction(camera, speed);
		keyLeft = new KeyStrafeLeftAction(camera, speed);

		event = new InputActionEvent();
	}

	public void setSpeed(float speed) {
		super.setSpeed(speed);
		keyUp.setSpeed(speed);
		keyLeft.setSpeed(speed);
	}

	public void performAction(InputActionEvent evt) {
		if (camera.isParallelProjection()) {
			int height = DisplaySystem.getDisplaySystem().getHeight();
			float scale = (camera.getFrustumTop() - camera.getFrustumBottom()) / height;

			if (grabButton == InputHandler.BUTTON_NONE || MouseInput.get().isButtonDown(grabButton)) {
				if (mouse.getLocalTranslation().x != 0) {
					event.setTime(mouse.getLocalTranslation().x * scale);
					keyLeft.performAction(event);
				}
				if (mouse.getLocalTranslation().y != 0) {
					event.setTime(mouse.getLocalTranslation().y * scale);
					keyUp.performAction(event);
				}
			}
		}
	}

	public int getGrabButton() {
		return grabButton;
	}

	public void setGrabButton(int grabButton) {
		this.grabButton = grabButton;
	}
	
}